using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace MyRobot
{
    public abstract class ProcessNode: Node
    {
        private PropertyClass _Properties;
        private string debugMessage;

        public new class PropertyClass : Node.PropertyClass
        {
            public int UpdateInterval;
        }

        public new PropertyClass Properties
        {
            get { return _Properties; }
            set { _Properties = value; }
        }

        private Thread _thread;

        public ProcessNode(string Name) : base(Name) 
        {
            _Properties = new PropertyClass();
        }

        public ProcessNode()
        {
            _Properties = new PropertyClass();
        }

        public virtual void Process() { }

        public virtual void Run_Node() { }

        public void Run()
        {
            debugMessage = "Starting new thread: " + this.Name;
            try
            {
                if (this.ThreadState == ThreadState.Unstarted || this.ThreadState == ThreadState.Stopped)
                {
                    _thread = new Thread(new ThreadStart(Run_Node));
                    _thread.IsBackground = false;
                    _thread.Start();
                }
            }
            catch
            {
                _thread = new Thread(new ThreadStart(Run_Node));
                _thread.IsBackground = false;
                _thread.Start();
            }
            MyRobot.myGUI.DebugMessage(MyRobot.ActiveForm, (Control)MyRobot.controlHashtable["DebugMessages"], debugMessage);

        }

        public Thread Thread
        {
            get { return _thread; }
        }

        public System.Threading.ThreadState ThreadState
        {
            get
            {
                try
                {
                    return _thread.ThreadState;
                }
                catch
                {
                    return System.Threading.ThreadState.Unstarted;
                }
            }
        }

        public void Stop()
        {
            debugMessage = "Stopping thread: " + this.Name;
            try
            {
                if (this.ThreadState != ThreadState.Unstarted)
                {
                    this.Thread.Abort();
                }
            }
            catch (System.Threading.ThreadAbortException)
            {
            }

            MyRobot.myGUI.DebugMessage(MyRobot.ActiveForm, (Control)MyRobot.controlHashtable["DebugMessages"], debugMessage);

            this.Properties.Status = Message.Status.Stopped;
        }

    }
}
